from plugin import plugin

__doc__ = "shopping"

class shopping(plugin):
	"Holds shopping lists"

	def __init__(self):
		self._version = '3.0 testing'
		self.lists = {}

	def shopping(self, msg, args):
		"Usage: .shopping add [item name]  (adds item), .shopping get  (returns all your items), .shopping get [name]  (get name's shopping list), .shopping clear  (clear your shopping list)"
		cmd, _, nargs = args.partition(' ')
		if cmd == '':
			self._irc.reply(msg, self.shopping.__doc__)
		else:
			if hasattr(self, '_' + cmd):
				try: getattr(self, '_' + cmd)(msg, nargs)
				except: self._irc.reply(msg, self.shopping.__doc__)
			else:
				self._irc.reply(msg, self.shopping.__doc__)

	def _add(self, msg, args):
		sender = msg['sender']['name']
		if sender not in self.lists:
			self.lists[sender] = []
		self.lists[sender] += args.split(',')

	def _get(self, msg, args):
		who = msg['sender']['name']
		if args != '':
			who = args
		if who in self.lists:
			self._irc.reply(msg, ', '.join(self.lists[who]))
		else:
			self._irc.reply(msg, who + ' doesn\'t need anything!')

	def _clear(self, msg, args):
		sender = msg['sender']['name']
		if sender in self.lists:
			del self.lists[sender]
		
